const onHttps = window.location.href.match(/https/)

const config = {
  agentSystem: onHttps ? 'https://c-c-a.jiahuagame.com/game-lobby/' : 'https://c-c-a.jiahuagame.com/game-lobby/',
  // agentSystem: onHttps ? 'https://lefantian.jiahuagame.com/game-lobby' : 'https://lefantian.jiahuagame.com/game-lobby'
  // agentSystem: onHttps ? '10.10.10.159:8191/game-lobby' : '10.10.10.159:8191/game-lobby'
}
// if (window.location.hostname.match(/(10.10.10)|(localhost)/)) {
//   // config.pre = 'https://10.10.10.107:8080/game-lobby'
//   config.agentSystem = 'https://lefantian.jiahuagame.com/game-lobby'
// } else if (window.location.hostname.match(/lefantian/)) {
//   config.agentSystem = 'https://lefantian.jiahuagame.com/game-lobby'
// } else {
//   config.agentSystem = 'https://c-c-a.jiahuagame.com/game-lobby'
// }

// const interceptedFetch = (url, opt) => {
//   fetch(url, opt).then(res => {
//     if (res.ok) {
//       return res.json()
//     }
//     throw new error('请求出错')
//   })
// }

const homeApi = {
  querySelf() {
    const url = `${config.agentSystem}/agent/apply/${Cookies.get('areaCode')}/inviter`;
    const opt = {
      method: 'GET',
      headers: {
        Authorization: Cookies.get('token'),
        'Content-type': 'application/json'
      }
    }
    return fetch(url, opt)
  },
  // URL：{path}/agent/apply/{areaCode}/{superuid}/inviter
  queryUpperAgent(send) {
    const url = `${config.agentSystem}/agent/apply/${Cookies.get('areaCode')}/${send.upperAgentID}/inviter`;
    const opt = {
      method: 'GET',
      headers: {
        Authorization: Cookies.get('token'),
        'Content-type': 'application/json'
      }
    }
    return fetch(url, opt)
  },
  // URL：{path}/agent/apply/{areaCode}
  apply(send) {
    const url = `${config.agentSystem}/agent/apply/${Cookies.get('areaCode')}`;
    const opt = {
      method: 'POST',
      headers: {
        Authorization: Cookies.get('token'),
        'Content-type': 'application/json'
      },
      body: JSON.stringify(send)
    }
    console.log('send', send)
    return fetch(url, opt)
  }
}

const payApi = {
  // URL：{path}/Order/shopping/{areaCode}/{goods_type}/{matket_type}
  getShoppingItems(level) {
    const url = `${config.agentSystem}/Order/shopping/${Cookies.get('areaCode')}/1/${level}`;
    const opt = {
      method: 'GET',
      headers: {
        Authorization: Cookies.get('token'),
        'Content-type': 'application/json'
      }
    }
    return fetch(url, opt)
  },
  // URL：{path}/agent/apply/{areaCode}/{orderNo}/audit
  audit(orderNo) {
    const url = `${config.agentSystem}/agent/apply/${Cookies.get('areaCode')}/${orderNo}/audit`;
    const opt = {
      method: 'GET',
      headers: {
        Authorization: Cookies.get('token'),
        'Content-type': 'application/json'
      }
    }
    return fetch(url, opt)
  },
  generateOrder(params, lftid) {
    const url = `${config.agentSystem}/Order/${Cookies.get('areaCode')}/${lftid}`
    const opt = {
      method: 'POST',
      body: JSON.stringify(params),
      headers: {
        Authorization: Cookies.get('token'),
        'Content-type': 'application/json'
      }
    }
    console.log(opt)
    return fetch(url, opt)
  },
}

export default { homeApi, payApi }